package cate.game.client.request;

import cate.game.GameBody;
import cate.game.client.msg.GameMsg;
import cate.game.client.msg.MsgAnnotationHandler;
import cate.game.client.msg.MsgInput;
import cate.game.framework.ModuleMan;


public class ClientRequestHandler extends ModuleMan {
    private static final MsgAnnotationHandler ANNOTATION_HANDLER = new MsgAnnotationHandler();
    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
        log.debug("--");
    }

    @Override
    public void initDependent() throws Exception {

    }

    public void onMsgInput(GameBody game, GameMsg msg) {
        // 这边的逻辑可以用线程池来约束
        MsgInput input = ANNOTATION_HANDLER.genMsgInput(msg);
        if(input != null) {
            input.handle(game, msg);
        }
    }

    /** 处理消息输入的异常 */
    public void onMsgInputException(GameBody game, GameMsg msg, Exception e) {
        if(msg != null) {
            log.error("msg a={}, b={}", msg.idA, msg.idB, e);
        } else {
            log.error("", e);
        }
    }

    private static final boolean COUNTER_ENABLED = true;
    private static final Counter COUNTER = new Counter();
    public static void countReqs(String name) {
        COUNTER.count(name);
    }
    public static int getReqsCount() {
        int count = COUNTER.getCount();
        COUNTER.reset();
        return count;
    }
    public static boolean countEnabled() {
        return COUNTER_ENABLED;
    }
}
